#! /bin/sh
# PCP QA Test No. 067
# Test PMCD's ability to detect and cleanup agents it didn't create when they
# terminate
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

which netstat >/dev/null 2>&1 || _notrun "netstat not installed"

rm -f $seq.out
_get_libpcp_config
if $ipv6 ; then
    ln $seq.out.ipv6 $seq.out || exit 1
else
    ln $seq.out.nonipv6 $seq.out || exit 1
fi

trap "rm -f $tmp.*; exit" 0 1 2 3 15

# real QA test starts here

signal=$PCP_BINADM_DIR/pmsignal
log=$PCP_PMCDLOG_PATH
LOCALHOST=`hostname`

_filter_log()
{
    sleep 1
    _filter_pmcd_log <$log \
    | sed \
	-e '/^$/d' \
	-e 's/ *$//' \
	-e 's/^$//' \
	-e '/^->/d' \
	-e '/^00[08]:/d' \
	-e '/^sample/s/.*/sample agent.../' \
	-e 's/fd=[0-9][0-9]*/fd=<n>/g' \
	-e 's/^  ok    [0-9]*/  ok  <n>/g' \
	-e '/^test/s/  *[0-9][0-9]*  *[0-9][0-9]* ... bin/ <n> <n> <n> bin/' \
	-e '/Resource busy/d' \
	-e 's/0x[^ ]*\([^ ][^ ][^ ]\)/ADDR...\1/' \
	-e '/client connection from/,/^$/d' \
	-e '/Cleanup/d' \
	-e '/Info: spindle_stats_init/d' \
	-e '/UNIX_DOMAIN_SOCKET/d' \
	#end
}

_filter_pmda_log()
{
    sed \
	-e 's/^\(Log for pmda[^ ]* on\) .*/\1 HOST .../' \
	-e 's/^\(Log finished\) .*/\1 .../'
}

interrupt()
{
    echo "Interrupted"
    _restore_auto_restart pmcd
    exit
}

if [ -d $PCP_LOG_DIR/pmlogger ]
then
    LOGGING_DIR=$PCP_LOG_DIR/pmlogger
else
    LOGGING_DIR=$PCP_LOG_DIR
fi

cleanup()
{
    _restore_config $PCP_PMCDCONF_PATH
    pmafm $LOGGING_DIR/$LOCALHOST/Latest remove >$tmp.cmd 2>&1 \
    && $sudo sh $tmp.cmd
    echo "Restarting pmcd"
    _service pcp restart 2>&1 | _filter_pcp_start
    _restore_auto_restart pmcd
    _wait_for_pmcd
    _wait_for_pmlogger
    rm -f $tmp.*
}

trap interrupt 1 2 3 15
trap cleanup 0

_stop_auto_restart pmcd

domain=246
if [ $PCP_PLATFORM = linux ]
then
    cat >$tmp.config <<End-of-file
# Installed by PCP QA test $seq on `date`
linux	60	dso	linux_init	$PCP_PMDAS_DIR/linux/pmda_linux.so
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.so
test	$domain	socket	inet 2222
End-of-file
elif [ $PCP_PLATFORM = darwin ]
then
    cat >$tmp.config <<End-of-file
# Installed by PCP QA test $seq on `date`
darwin	78	dso	darwin_init	$PCP_PMDAS_DIR/darwin/pmda_darwin.dylib
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.dylib
test	$domain	socket	inet 2222
End-of-file
elif [ $PCP_PLATFORM = solaris ]
then
    cat >>$tmp.config <<End-of-File
# Installed by PCP QA test $seq on `date`
solaris	75	dso	solaris_init	$PCP_PMDAS_DIR/solaris/pmda_solaris.so
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.so
test	$domain	socket	inet 2222
End-of-File
elif [ $PCP_PLATFORM = freebsd ]
then
    cat >>$tmp.config  <<End-of-File
# Installed by PCP QA test $seq on `date`
freebsd	85	dso	freebsd_init	$PCP_PMDAS_DIR/freebsd/pmda_freebsd.so
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.so
test	$domain	socket	inet 2222
End-of-File
else
    echo "Error - need pmcd.conf setup for $PCP_PLATFORM"
    exit 1
fi

# netstat -an formats
#
# Redhat 7.3 and 9
# tcp        0      0 0.0.0.0:2222            0.0.0.0:*               LISTEN
#
# Mac OS X 10.6
# tcp4       0      0  *.2222                 *.*                    LISTEN
#
# openindiana
#      *.2222               *.*                0      0 128000      0 LISTEN
#

$sudo $signal -a pmgadgets pmchart pmview pmie >/dev/null 2>&1
src/pmdashutdown -i 2222 -d $domain -l $tmp.pmdalog &
pid=$!
echo "pmdashutdown pid: $pid" >$seq.full
sleep 1
echo
echo "port 2222 should be listening"
netstat -an \
| tee -a $seq.full \
| grep '[.:]2222 ' \
| sed \
    -e 's/127\.0\.0\.1/*/g' \
    -e 's/0\.0\.0\.0/*/g' \
    -e 's/:/./g' \
    -e 's/.*\(\*.2222\).*\(LISTEN\).*/... \1 ... \2/'
echo

_save_config $PCP_PMCDCONF_PATH
$sudo cp $tmp.config $PCP_PMCDCONF_PATH
pmafm $LOGGING_DIR/$LOCALHOST/Latest remove >$tmp.cmd 2>&1 \
&& $sudo sh $tmp.cmd
_service pcp stop | _filter_pcp_stop
# Note: need to use $PCP_RC_DIR/* here, not _service pcp so that
# systemd (if running) does not get to see the pmcd exit status
#
( $sudo $PCP_RC_DIR/pmcd start; $sudo $PCP_RC_DIR/pmlogger start ) 2>&1 | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger
# echo "config:"
# cat $config | sed -e "s;$PCP_PMDAS_DIR;\$PCP_PMDAS_DIR;"
echo

# wait for pmdashutdown to finish
#
for t in 1 2 3 4 5 6
do
    echo >>$seq.full
    echo "wait for pmdashutdown to finish ... cycle $t" >>$seq.full
    if ps $PCP_PS_ALL_FLAGS | tee -a $seq.full | cut -f1 | grep $pid >/dev/null 2>&1
    then
	sleep 2
    else
	break
    fi
done

echo >>$seq.full
cat $tmp.pmdalog >>$seq.full
echo
echo "pmda log (should have terminated):"
_filter_pmda_log <$tmp.pmdalog
echo

$sudo $signal -a -s HUP pmcd

# wait for PMCD to finish writing log (or 5 seconds)
for t in 1 2 3 4 5
do
    if egrep '^Cleanup "test" agent' $log >/dev/null 2>&1
    then
	break
    fi
    sleep 1
done

_filter_log
